﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using Microsoft.SharePoint;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.Administration;
using System.Reflection;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.WebControls;

namespace Macaw.Moss2007.TimerJobServerSelectorSolution
{
    public partial class ServerSelectorControl : UserControl
    {
        protected CheckBox cbServerSelected;
        protected ServerSelector selector;

        public Microsoft.SharePoint.ApplicationPages.JobEditPage EditPage { get; set; }

        protected SPJobDefinition CurrentJob
        {
            get
            {
                PropertyInfo CurrentJobPI = typeof(JobEditPage).GetProperty("CurrentJob", BindingFlags.NonPublic | BindingFlags.Instance);
                if (CurrentJobPI == null) throw new Exception("CurrentJob property not found");

                return (SPJobDefinition)CurrentJobPI.GetValue(EditPage, null);
            }
        }

        protected SPServer CurrentJobServer
        {
            set
            {
                FieldInfo CurrentJobServerFI = typeof(SPJobDefinition).GetField("m_Server", BindingFlags.NonPublic | BindingFlags.Instance);
                if (CurrentJobServerFI == null) throw new Exception("m_Server field not found");
                CurrentJobServerFI.SetValue(CurrentJob, value);
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        protected override void CreateChildControls()
        {
            // Check wether this page is not a postback and it's also a refresh due to a server select
            if (!this.Page.IsPostBack && string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.Params["ServerId"]))
            {
                try
                {

                    if (CurrentJob.Server != null)
                    {
                        selector.CurrentId = CurrentJob.Server.Id.ToString().Trim(new char[] { '{', '}' });
                        cbServerSelected.Checked = true;
                    }
                    else
                    {
                        cbServerSelected.Checked = false;
                    }
                }
                catch
                {

                }
            }
            else
            {
                if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.Params["ServerId"]))
                {
                    cbServerSelected.Checked = true;
                }

                string cbstatus = System.Web.HttpContext.Current.Request.Params[cbServerSelected.UniqueID];
                if (cbstatus != null && cbstatus.Equals("on", StringComparison.InvariantCultureIgnoreCase))
                {
                    cbServerSelected.Checked = true;
                }

                if (HttpContext.Current.Request.Params["__EVENTTARGET"]!=null && HttpContext.Current.Request.Params["__EVENTTARGET"].Contains("BtnEditJob"))
                {
                    Guid CurrentId = new Guid(selector.CurrentId);
                    SPServer server = null;
                    if (cbServerSelected.Checked)
                    {
                        foreach (SPServer s in SPFarm.Local.Servers)
                        {
                            if (s.Id.Equals(CurrentId))
                            {
                                server = s;
                            }
                        }
                        if (server != null)
                        {
                            CurrentJobServer = server;
                        }
                    }
                    else
                    {
                        CurrentJobServer = null;
                    }
                }
            }
            base.CreateChildControls();
        }
    }
}